<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="form" :model="userInfo" :rules="rules" label-width="100px">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门" prop="departmentName">
            <el-input
              v-model="userInfo.departmentName"
              style="width: 50%"
              placeholder="请选择部门"
              @focus="showDept = true"
            />
            <!--
          需求：选择部门，显示到上边输入框
         -->
            <el-row v-show="showDept" class="selBox">
              <!-- 关闭按钮 -->
              <i class="el-icon-circle-close" @click="showDept = false" />
              <el-tree
                ref="tree"
                :data="departData"
                :props="{ label: 'name' }"
                @node-click="selDept"
              ></el-tree>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机" prop="mobile">
            <el-input v-model="userInfo.mobile" disabled style="width: 50%" />
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间" prop="timeOfEntry">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
            <!-- .sync相当于子改父的@事件 用法一样 简单形式 -->
            <UploadImg :staff-photo.sync="userInfo.staffPhoto" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="updatedepartmentName">
            保存更新
          </el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { saveUserDetailById } from '@/api/employees'
// 登录人信息
import { mapActions } from 'vuex'
// 部门选择
import { getDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      // 控制部门选择显示隐藏
      showDept: false,
      // 部门数据
      departData: [],
      rules: {
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getDepartData()
  },
  methods: {
    ...mapActions('user', ['getUserInfoAction']),
    // 选择部门
    selDept (currDept) {
      console.log('当前选中的部门：', currDept)
      this.userInfo.departmentName = currDept.name
      // 选完关闭
      this.showDept = false
    },
    // 获取部门数据
    async getDepartData () {
      const { depts } = await getDepartments()
      this.departData = transformTreeData(depts)
    },
    async updatedepartmentName () {
      try {
        await this.$refs.form.validate()
        await saveUserDetailById(this.userInfo)
        // 更新右上角显示的登录人信息=》重新调用 vuex的action方法
        this.getUserInfoAction()
        this.$message.success('更新成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
